<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>行首行尾插入指定字符</title>
</head>
<body>
<textarea name="" id="text" cols="30" rows="10"></textarea><br>
前插入 <input type="text" id="before"><br>
后插入 <input type="text" id="after"><br>
<input type="button" id="btn" value="插入">
<script>
    var oText = document.getElementById("text");
    var oBefore = document.getElementById("before");
    var oAfter = document.getElementById("after");
    var oBtn = document.getElementById("btn");
    oBtn.onclick = function () {
        oText.value = oBefore.value + oText.value.replace(/\n/g, oAfter.value + "\n" + oBefore.value) + oAfter.value;
    }
</script>
</body>
</html>